package com.fili.gui;

import com.fili.SharedData;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

/**
 * Date: Nov 16, 2005
 * Time: 6:53:08 PM
 *
 * @author Claudiu-Florin FILIMON for Borux Consulting S.R.L.
 */
public class BaseConfigDialog extends JDialog implements ComponentListener
{
	protected JButton btnOK;
	protected JButton btnCancel;
	protected JButton btnUndo;

	protected ComponentContentSaver ccSaver;

	protected JPanel mainPanel;

	private boolean okClicked;

/*-------------------------------------------------------------------------------------
	Getters and setters
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
	Constructors
 --------------------------------------------------------------------------------------*/

	protected BaseConfigDialog(Frame owner, String title, String windowName, Dimension panelSize)
	{
		super(owner, title, true);

        new ComponentPositionSaver(this, windowName, new Rectangle(10, 10, panelSize.width + GUIData.getDifX(), panelSize.height + GUIData.getDifY() + 36));
		ccSaver = new ComponentContentSaver(SharedData.getPEngine(), windowName);

		setResizable(false);

		JPanel pnlButtons = new JPanel(new GridLayout(1, 2, 5, 0));
		pnlButtons.setPreferredSize(new Dimension(125, 26));


		btnOK = new JButton("OK");
		btnOK.setMargin(new Insets(0, 0, 0, 0));
		btnOK.setDefaultCapable(true);
		getRootPane().setDefaultButton(btnOK);
		btnOK.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				onOKClicked();
			}
		});

		pnlButtons.add(btnOK);

		btnCancel = new JButton("Cancel");
		btnCancel.setMargin(new Insets(0, 0, 0, 0));
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				onCancelClicked();
			}
		});
		pnlButtons.add(btnCancel);

		JPanel pnlBottom = new JPanel(new BorderLayout());
		pnlBottom.add(pnlButtons, BorderLayout.EAST);

		btnUndo = new JButton("  Undo  ");
		btnUndo.setMargin(new Insets(0, 0, 0, 0));
		btnUndo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				onDataUndone();
			}
		});
		pnlBottom.add(btnUndo, BorderLayout.WEST);

		pnlBottom.setBorder(new LineBorder(pnlBottom.getBackground(), 5));
		setLayout(new BorderLayout());
		add(pnlBottom, BorderLayout.SOUTH);

		mainPanel = new JPanel(null);
		add(mainPanel, BorderLayout.CENTER);
	}
/*-------------------------------------------------------------------------------------
    Methods
 --------------------------------------------------------------------------------------*/

	protected void onDataUndone()
	{
		ccSaver.undoChanges();
	}

	protected void onCancelClicked()
	{
		okClicked = false;
		setVisible(false);
	}

	protected void onOKClicked()
	{
		okClicked = true;
		setVisible(false);
		ccSaver.saveAll();
	}

	public boolean showModal()
	{
		okClicked = false;
		setVisible(true);
		return okClicked;
	}

	public JLabel addLabel(String text, int x, int y)
	{
		JLabel lbl = new JLabel(text);
		lbl.setLocation(x, y);
		lbl.setSize(lbl.getPreferredSize());

		mainPanel.add(lbl);

		return lbl;
	}

	public JLabel addLabel(String text, Point location)
	{
		return addLabel(text, location.x, location.y);
	}

	public JCheckBox addCheckbox(String text, boolean selected, String name, int x, int y)
	{
		JCheckBox cb = new JCheckBox(text);
		ccSaver.addComponent(cb, name, selected);
		cb.setLocation(x, y);
		cb.setSize(cb.getPreferredSize());

		mainPanel.add(cb);

		return cb;
	}

	public JCheckBox addCheckbox(String text, boolean selected, String name, Point location)
	{
		return addCheckbox(text, selected, name, location.x, location.y);
	}

/*-------------------------------------------------------------------------------------
    Overrides
 --------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------
    Implementations
 --------------------------------------------------------------------------------------*/

	public void componentResized(ComponentEvent e)
	{
		int w = getWidth();
		int h = getHeight();
		boolean resize = false;

		if (w < 300)
		{
			w = 300;
			resize = true;
		}
		if (h < 200)
		{
			h = 200;
			resize = true;
		}

		if (resize)
			setSize(w, h);
	}

	public void componentMoved(ComponentEvent e)
	{
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void componentShown(ComponentEvent e)
	{
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void componentHidden(ComponentEvent e)
	{
		//To change body of implemented methods use File | Settings | File Templates.
	}
}
